package com.yhyvr.api.tool;


import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

public class ToolHibernateValidate {
	private static Validator validator;
	
	static{
		   ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		   validator = factory.getValidator();
	}
	
	public static <T> String getResault(T t){
		if(t==null){
			return null;
		}else{
		  Set<ConstraintViolation<T>> constraintViolations =
				  validator.validate(t);
			StringBuilder errorMsgBuiler = new StringBuilder();
     	  if (constraintViolations!=null&&constraintViolations.size()!=0) {
		  	 for (ConstraintViolation violation : constraintViolations) {
			   errorMsgBuiler.append(violation.getPropertyPath());
			   errorMsgBuiler.append(":");
			   errorMsgBuiler.append(violation.getMessage());
			   errorMsgBuiler.append(". ");
		   	}
		   return errorMsgBuiler.toString();
	   }else {
		   return  null;
	   }
		}
	}
}
